package app.book;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

public class Book {

    @JsonProperty("name") private String name;
    @JsonProperty("category") private BookCategory category;
    @JsonProperty("readdate") private String readDate;
    @JsonProperty("id")  private String id;

    public Book() {
        this.name = "";
        this.category = BookCategory.Chinese;
        this.readDate = "";
        this.id = "";
    }

    public String getName() {
    	return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BookCategory getCategory() {
        return category;
    }

    public void setCategory(BookCategory category) {
        this.category = category;
    }

    public String getReadDate() {
        return readDate;
    }

    public void setReadDate(String readDate) {
        this.readDate = readDate;
    }

    public void setReadDateToday() {
        this.readDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void generateUUID() {
        this.id = UUID.randomUUID().toString();
    }

    public String toJsonString() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
    }

    public String categoryString() {
        return this.category.toString();
    }
}
